const { Op } = require("sequelize");

const moment = require('moment');

const projectDetails = require("../model/projectDetail")
// // 参数： id ， 项目名称 ， 项目进度，数量，交付日期，状态，项目人员
class projectDetailService {
    // 创建项目
    async createProject({ projectName, projectProgress, projectLeader, deliveryDate, status, projectPersonnel }) {
        const res = await projectDetails.create({
            projectProgress,
            projectName,
            projectLeader,
            deliveryDate,
            status,
            projectPersonnel
        })
        return res
    }
    // 分页 获取项目，不包含已删除的项目
    async getProjectByPage({ pageSize, currentPage }) {
        const res = await projectDetails.findAndCountAll(
            {
                limit: parseInt(pageSize),
                offset: (currentPage - 1) * parseInt(pageSize),
                where: {
                    isDelete: false
                }
            }
        )
        return res
    }
    // 获取项目,并且不包含已删除的项目
    async getProject({ id }) {
        const res = await projectDetails.findAll({
            where: {
                id,
                isDelete: false
            }
        })
        return res
    }
    // 删除项目 将isDelte字段置为true
    async deleteProject({ id }) {
        console.log(id, '?????');
        const res = await projectDetails.update({
            isDelete: true
        }, {
            where: {
                id,
                isDelete: false
            }
        })

        return res
    }
    // 编辑项目
    async editProject({ id, projectName, projectLeader, projectProgress, number, deliveryDate, status, projectPersonnel }) {
        const res = await projectDetails.update({
            projectName,
            projectLeader
        }, {
            where: {
                id,
                isDelete: false
            }
        })
        return res
    }
    // 可以通过某一年，某一周，某一月来获取项目，可以不传参数，分页获取，不包含已删除的项目
    async getProjectByDate({ pageSize, currentPage, year, week, month }) {
        let res = null;
        const currentDate = moment(); // 获取当前日期

        // 如果年份、周数和月份都没有传入，则查询当前年的数据
        if (!year && !week && !month) {
            const startOfYear = currentDate.startOf('year').format('YYYY-MM-DD');
            const endOfYear = currentDate.endOf('year').format('YYYY-MM-DD');
            res = await projectDetails.findAndCountAll({
                limit: parseInt(pageSize),
                offset: (currentPage - 1) * parseInt(pageSize),
                where: {
                    isDelete: false,
                    createdAt: {
                        [Op.gte]: startOfYear,
                        [Op.lte]: endOfYear
                    }
                }
            });
        }
        // 如果传入了年份参数，则查询该年的数据
        else if (year) {
            const startOfYear = moment().year(year).startOf('year').format('YYYY-MM-DD');
            const endOfYear = moment().year(year).endOf('year').format('YYYY-MM-DD');
            res = await projectDetails.findAndCountAll({
                limit: parseInt(pageSize),
                offset: (currentPage - 1) * parseInt(pageSize),
                where: {
                    isDelete: false,
                    createdAt: {
                        [Op.gte]: startOfYear,
                        [Op.lte]: endOfYear
                    }
                }
            });
        }
        // 如果传入了周数参数，则查询该周的数据
        else if (week) {
            const startOfWeek = moment(currentDate).isoWeek(week).startOf('isoWeek').format('YYYY-MM-DD');
            const endOfWeek = moment(currentDate).isoWeek(week).endOf('isoWeek').format('YYYY-MM-DD');
            console.log(startOfWeek, endOfWeek);
            res = await projectDetails.findAndCountAll({
                limit: parseInt(pageSize),
                offset: (currentPage - 1) * parseInt(pageSize),
                where: {
                    isDelete: false,
                    createdAt: {
                        [Op.gte]: startOfWeek,
                        [Op.lte]: endOfWeek
                    }
                }
            });
        }
        // 如果传入了月份参数，则查询该月的数据
        else if (month) {
            const startOfMonth = moment().month(month - 1).startOf('month').format('YYYY-MM-DD');
            const endOfMonth = moment().month(month - 1).endOf('month').format('YYYY-MM-DD');
            res = await projectDetails.findAndCountAll({
                limit: parseInt(pageSize),
                offset: (currentPage - 1) * parseInt(pageSize),
                where: {
                    isDelete: false,
                    createdAt: {
                        [Op.gte]: startOfMonth,
                        [Op.lte]: endOfMonth
                    }
                }
            });
        }
        // 如果以上情况都不满足，则查询当前月的数据
        else {
            const startOfMonth = currentDate.startOf('month').format('YYYY-MM-DD');
            const endOfMonth = currentDate.endOf('month').format('YYYY-MM-DD');
            res = await projectDetails.findAndCountAll({
                limit: parseInt(pageSize),
                offset: (currentPage - 1) * parseInt(pageSize),
                where: {
                    isDelete: false,
                    createdAt: {
                        [Op.gte]: startOfMonth,
                        [Op.lte]: endOfMonth
                    }
                }
            });
        }
        return res;
    }



}

module.exports = projectDetailService;
